package com.spring.sunflower.login

import android.content.Intent
import android.text.TextUtils
import android.widget.LinearLayout
import android.widget.TextView
import com.lxj.xpopup.XPopup
import com.spring.sunflower.App
import com.spring.sunflower.MainActivity
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseActivity
import com.spring.sunflower.bean.BalanceBean
import com.spring.sunflower.bean.WeChatH5Bean
import com.spring.sunflower.bean.WeChatH5PayResultBean
import com.spring.sunflower.bean.WeChatPayBean
import com.spring.sunflower.common.WebViewH5PayActivity
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.util.AppUtils
import com.spring.sunflower.util.PayUtils
import com.spring.sunflower.widget.BottomPayDialog
import java.util.*

/**
 * Created by Kevin on 2021/9/18<br/>
 *
 * Describe:<br/>
 */
class DoorActivity : BaseActivity<DoorPresenter>(), LoginContract.DoorView {
    private var llJoin: LinearLayout? = null
    private var tvPrice: TextView? = null
    private var mData: BalanceBean.Data? = null
    private var payUtils: PayUtils? = null
    private var userID: String = ""
    private var shouldCheckIfPaySuccess = false//是否调用判断支持成功接口
    private var mOrderId = ""
    private var currentEnvironment: String = ""
    override fun getLayoutID(): Int = R.layout.activity_door

    override fun setPresenter(): DoorPresenter = DoorPresenter(this)

    override fun initView() {
        currentEnvironment = getField(Constant.KEY_ENVIRONMENT, Constant.ENVIRONMENT_PRODUCT)
        userID = getField(Constant.KEY_USERID, "")
        llJoin = findViewById(R.id.llJoin)
        tvPrice = findViewById(R.id.tvPrice)
        payUtils = PayUtils.getInstance(this)
        mPresenter.getExchangeID()
        // 在调用TBS初始化、创建WebView之前进行如下配置
        // 在调用TBS初始化、创建WebView之前进行如下配置
        (application as App).initSDK()
        val bottomPayDialog = BottomPayDialog(this)
        bottomPayDialog.setOnPaySelectListener(object : BottomPayDialog.OnPaySelectListener {
            override fun onPaySelect(position: Int) {
//                showToast(if (position == 0) "支付宝" else "微信")
                if (mData == null) {
                    showToast("当前无法支付，请退出重新进入")
                    return
                }
                if (position == 0) {
                    mPresenter.aliPayGenerate(mData!!.id)
//                    mPresenter.weChatPayH5(mData!!.id,userID)
                } else {
                    if (AppUtils.isAppInstalled(this@DoorActivity, "com.tencent.mm")) {
//                        mPresenter.weChatPayGenerate(mData!!.id)
                        val map: MutableMap<String, String> = HashMap()
                        map["exchangeId"] = mData!!.id
                        map["type"] = "1"
                        map["token"] = getField(Constant.KEY_TOKEN, "")
                        map["MID"] = userID
                        map["userIdOrMobile"] = userID
                        if (currentEnvironment == Constant.ENVIRONMENT_PRODUCT) {
                            mPresenter.getWeChatH5PayProduct(map)
                        } else {
                            mPresenter.getWeChatH5Pay(map)
                        }
                    } else {
//                        mPresenter.weChatPayH5()
                        showToast("微信未安装")
                    }
                }
                bottomPayDialog.dismiss()
            }

        })
        llJoin?.setOnClickListener {
            XPopup.Builder(this)
                .enableDrag(false)
                .asCustom(
                    bottomPayDialog
                )
                .show()
        }
        payUtils?.setOnPayResultListener {
            startNewActivity(MainActivity::class.java)
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        if (shouldCheckIfPaySuccess && !TextUtils.isEmpty(mOrderId)) {
            mPresenter.getWeChatH5PayResult(mOrderId)
            shouldCheckIfPaySuccess = false
        }
    }

    override fun showExchangeList(data: MutableList<BalanceBean.Data>?) {
        if (data!!.size > 0) {
            val data1 = data[0]
            mData = data1
            tvPrice!!.text = "￥${data1.money}"
        }
    }

    override fun showAliGenerateData(data: String?) {
        payUtils?.payWithAlipay(data)
    }

    override fun showAliGenerateH5Data(data: String?) {
    }

    override fun showAliGenerateVipData(data: String?) {
    }

    override fun showWeChatGenerateData(data: WeChatPayBean.DatasBean?) {
        payUtils?.payWithWeChatPay(data!!)
    }

    override fun showWeChatGenerateH5Data(data: WeChatPayBean.DatasBean?) {
    }

    override fun showWeChatGenerateVipData(data: WeChatPayBean.DatasBean?) {
    }

    override fun showWeChatH5Data(data: WeChatH5Bean.DatasBean?) {
        val mwebUrl = data!!.mwebUrl
        mOrderId = data!!.orderNo
        val intent = Intent(this, WebViewH5PayActivity::class.java)
        intent.putExtra("mWebUrl", mwebUrl)
        startActivity(intent)
        shouldCheckIfPaySuccess = true
    }

    override fun showWeChatH5PayResult(data: WeChatH5PayResultBean.DatasBean?) {
        when (data!!.status) {
            "0" -> {
                showToast("未完成支付")
            }
            "1" -> {
                showToast("支付成功")
                startNewActivity(MainActivity::class.java)
                finish()
            }
            "2" -> {
                showToast("取消支付")
            }
            "3" -> {
                showToast("订单已验证")
            }
            "4" -> {
                showToast("订单号不存在")
            }
            "5" -> {
                showToast("人工确认-已充值")
            }
        }
    }
}